package com.ruoyi.knowledge.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.knowledge.domain.QualityDiseasesCatalogue;
import com.ruoyi.knowledge.service.IQualityDiseasesCatalogueService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 常见质量通病目录Controller
 * 
 * @author ruoyi
 * @date 2024-07-14
 */
@RestController
@RequestMapping("/knowledge/knowledgeCatalogue")
public class QualityDiseasesCatalogueController extends BaseController
{
    @Autowired
    private IQualityDiseasesCatalogueService qualityDiseasesCatalogueService;

    /**
     * 查询常见质量通病目录列表
     */
    @PreAuthorize("@ss.hasPermi('knowledge:knowledgeCatalogue:list')")
    @GetMapping("/list")
    public TableDataInfo list(QualityDiseasesCatalogue qualityDiseasesCatalogue)
    {
        startPage();
        List<QualityDiseasesCatalogue> list = qualityDiseasesCatalogueService.selectQualityDiseasesCatalogueList(qualityDiseasesCatalogue);
        return getDataTable(list);
    }

    /**
     * 导出常见质量通病目录列表
     */
    @PreAuthorize("@ss.hasPermi('knowledge:knowledgeCatalogue:export')")
    @Log(title = "常见质量通病目录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QualityDiseasesCatalogue qualityDiseasesCatalogue)
    {
        List<QualityDiseasesCatalogue> list = qualityDiseasesCatalogueService.selectQualityDiseasesCatalogueList(qualityDiseasesCatalogue);
        ExcelUtil<QualityDiseasesCatalogue> util = new ExcelUtil<QualityDiseasesCatalogue>(QualityDiseasesCatalogue.class);
        util.exportExcel(response, list, "常见质量通病目录数据");
    }

    /**
     * 获取常见质量通病目录详细信息
     */
    @PreAuthorize("@ss.hasPermi('knowledge:knowledgeCatalogue:query')")
    @GetMapping(value = "/{catalogueId}")
    public AjaxResult getInfo(@PathVariable("catalogueId") Long catalogueId)
    {
        return success(qualityDiseasesCatalogueService.selectQualityDiseasesCatalogueByCatalogueId(catalogueId));
    }

    /**
     * 新增常见质量通病目录
     */
    @PreAuthorize("@ss.hasPermi('knowledge:knowledgeCatalogue:add')")
    @Log(title = "常见质量通病目录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QualityDiseasesCatalogue qualityDiseasesCatalogue)
    {
        return toAjax(qualityDiseasesCatalogueService.insertQualityDiseasesCatalogue(qualityDiseasesCatalogue));
    }

    /**
     * 修改常见质量通病目录
     */
    @PreAuthorize("@ss.hasPermi('knowledge:knowledgeCatalogue:edit')")
    @Log(title = "常见质量通病目录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QualityDiseasesCatalogue qualityDiseasesCatalogue)
    {
        return toAjax(qualityDiseasesCatalogueService.updateQualityDiseasesCatalogue(qualityDiseasesCatalogue));
    }

    /**
     * 删除常见质量通病目录
     */
    @PreAuthorize("@ss.hasPermi('knowledge:knowledgeCatalogue:remove')")
    @Log(title = "常见质量通病目录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{catalogueIds}")

    public AjaxResult remove(@PathVariable Long[] catalogueIds)
    {
        //验证是否有子节点
         Long CataId = qualityDiseasesCatalogueService.queryIdByPid(catalogueIds[0]);
         if(CataId != null){
             return AjaxResult.warn("该目录下有子目录,请先删除子目录");
         }

        Long CataId1 = qualityDiseasesCatalogueService.queryIdByPid1(catalogueIds[0]);
        if(CataId1 != null){
            return AjaxResult.warn("常见通病目录详情已使用,不能删除");
        }
        return toAjax(qualityDiseasesCatalogueService.deleteQualityDiseasesCatalogueByCatalogueIds(catalogueIds));
    }

    //查询一个树结构
    @GetMapping("/getTree")
    public AjaxResult getTree()
    {
        //返回树的根节点查
        QualityDiseasesCatalogue root = qualityDiseasesCatalogueService.getTree();
//        List.of(root);
        //只返回根节点,页面 获取 根节点后 再加以一个[根节点]的壳,就可以实现[{}]结构了
        return success(root);
    }
}
